let app = getApp();
let API_URL = app.globalData.API_URL;
import { getTutor, getTutorDetail } from "../../../request/api.js";
import dayjs from "../../../common/js/dayjs.js";
let iconUrl = 'https://atush.vip/static/icons/';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        isAuthenticate: true,
        topTapCurrentIndex: 0,
        currentIndex: 0,
        showAuthDialog: false,
        showUnfilledDialog: false,
        showSuccessPromptDialog: false,
        tutor: [],
        review: [],
        review1: [],
        tips: {
            title_ug: 'سەمىمىي ئەسكەرتىش',
            title_zh: '温馨提示',
            content_ug: 'ئاتۇش ئۇچۇر سۇپىمىز شەخسىي ئۇچۇرىڭىزنى قوغدايدۇ .سىز ئۈچۈن تېخىمۇ ئىشەنچىلىك ، بىخەتەر بولغان ئۇچۇر تەمىنلەش ئۈچۈن ئۇچۇر يوللاشتىن بۇرۇن سالاھىتىڭىزنى دەلىللەڭ. سالاھىيەت دەلىللەنگەندىن كىيىن ئۇچۇر سۇپىمىزغا ئۇچۇر يوللىيالايسىز .',
            content_zh: '阿图什信息平台保护您的隐私。为了更提供高质量，可靠的信息发布信息前需要实名认证。'
        },
        currentItemIndex: null,
        editId: null,
        destroyId: null,
        userId: null,
        iconUrl: iconUrl,

        // 分页
        pageNo: 1,
        totalPage: 0,

        // 填空
        infoObj :{}
    },

    /**
     * My Function
     */

    // 发送
    sendBTN() {
        let id = this.data.infoObj.id
        let userId = this.data.userId
        let name_ug = this.data.infoObj.name_ug
        let price = this.data.infoObj.price
        let lectureRange_ug = this.data.infoObj.lectureRange_ug
        let graduatedSchool = this.data.infoObj.graduatedSchool
        let profession = this.data.infoObj.profession
        let education = this.data.infoObj.education
        let graduationTime = this.data.infoObj.graduationTime
        let contactPerson_ug = this.data.infoObj.contactPerson_ug
        let phoneNum = this.data.infoObj.phoneNum
        let wechat = this.data.infoObj.wechat
        let detailed_ug = this.data.infoObj.detailed_ug
        let data = {
            id: id,
            user_id: userId,
            name_ug,
            lectureRange_ug,
            price,
            profession,
            graduationTime,
            graduatedSchool,
            education,
            detailed_ug,
            contactPerson_ug,
            wechat,
            phoneNum
        }
        if (
            name_ug == undefined ||
            name_ug == '' ||
            price == undefined ||
            price == '' ||
            lectureRange_ug == undefined ||
            lectureRange_ug == '' ||
            graduatedSchool == undefined ||
            graduatedSchool == '' ||
            profession == undefined ||
            profession == '' ||
            education == undefined ||
            education == '' ||
            graduationTime == undefined ||
            graduationTime == '' ||
            contactPerson_ug == undefined ||
            contactPerson_ug == '' ||
            phoneNum == undefined ||
            phoneNum == '' ||
            wechat == undefined ||
            wechat == '' ||
            detailed_ug == undefined ||
            detailed_ug == ''
        ) {
            return this.setData({ showUnfilledDialog: true })
        }
        let that = this
        wx.request({
            url: `${API_URL}/tutor`,
            data: data,
            method: 'post',
            success: function () {
                that.setData({ showSuccessPromptDialog: true ,infoObj: {} });
            }
        })
    },

    name_ug(e){
        this.data.infoObj.name_ug = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    price(e){
        this.data.infoObj.price = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    lectureRange_ug(e){
        this.data.infoObj.lectureRange_ug = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    graduatedSchool(e){
        this.data.infoObj.graduatedSchool = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    profession(e){
        this.data.infoObj.profession = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    education(e){
        this.data.infoObj.education = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    graduationTime(e){
        this.data.infoObj.graduationTime = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    contactPerson_ug(e){
        this.data.infoObj.contactPerson_ug = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    phoneNum(e){
        this.data.infoObj.phoneNum = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    wechat(e){
        this.data.infoObj.wechat = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },

    detailed_ug(e){
        this.data.infoObj.detailed_ug = e.detail.value
        this.setData({infoObj : this.data.infoObj})
    },
    
    /**
     * 删除发布内容
     */
    // 获取 Id
    getIndexById(id, arr) {
        this.setData({ destroyId: id })
        for( let i in arr ) {
            if( id == arr[i].id ) {
                return i;
            }
        }
        return -1;
    },

    // 确定删除对
    dialogOnConfirm() {
        let token = null;
        let destroyId = this.data.destroyId;
        wx.getStorage({
            key: 'userInfo',
            success(res) {
                token = res.data.token
                wx.request({
                    url: `${API_URL}/tutorDestroy`,
                    header: {
                        token: res.data.token
                    },
                    data: { postId: destroyId },
                    method: 'post',
                    success: function (res) {
                        console.log('res: ', res);
                    }
                })
            }
        })

        if (this.data.currentItemIndex) {
            let index = this.data.currentItemIndex
            let arr = this.data.review
            arr.splice(index, 1)
            this.setData({ review: arr })
        }
        this.setData({ showAuthDialog: false })
    },
    
    // 取消删除
    dialogOnCancel() {
        this.setData({ showAuthDialog: false })
    },

    // 弹出删除对话框
    showDialogtTap(e) {
        let item = e.currentTarget.dataset.item
        this.setData({
            showAuthDialog: true,
            currentItemIndex: this.getIndexById(item.id, this.data.review)
        })
    },

    // 完毕对话框
    dialogOnComplete() {
        this.setData({ showUnfilledDialog: false })
    },

    // 成功提示框完毕
    completeDialog() {
        this.setData({ showSuccessPromptDialog: false })
    },

    // 编辑内容 
    onEdit(item) {
        let postId = item.currentTarget.dataset.item.id
        let data = { postId }
        getTutorDetail(res => {
            let editId = res.data.id
            this.setData({ currentIndex: 1, infoObj: res.data, editId: editId })
        }, data);
    },

    // 首页
    homePage() {
        let status = 1
        let data = { status, pageNo : this.data.pageNo }
        getTutor(res => {
            let request = res.data.data
            if (res.status == 200) {
                this.setData({ tutor: this.data.tutor.concat(request), totalPage : res.data.totalPage })
            }
        }, data);
    },

    // 全部发送
    allPush() {
        let status = -1
        let data = { status }
        getTutor(res => {
            let request = res.data.data
            if (res.status == 200) {
                for (let item of res.data.data) {
                    item.releaseTime = dayjs(item.releaseTime).format("YYYY-MM-DD")
                }
            }
            this.setData({ review: request })
        }, data);
    },

    // 待审核
    pendingReview() {
        let status = 0
        let data = { status }
        getTutor(res => {
            let request = res.data.data
            if (res.status == 200) {
                let arr = res.data
                for (let item of res.data.data) {
                    item.releaseTime = dayjs(item.releaseTime).format("YYYY-MM-DD")
                }
            }
            this.setData({ review1: request })
        }, data);
    },

    //用户点击bottomTab时调用
    titleClick: function (e) {
        let index = e.currentTarget.dataset.idx
        this.setData({ currentIndex: e.currentTarget.dataset.idx, pageNo: 1, tutor: [] });
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 0
        })

        switch (index) {
            case '0':
                this.homePage()
                break;
            case '1':
                this.setData({ userId: wx.getStorageSync('uid') })
                break;
            case '2':
                this.allPush()
                break;
            default:
                break;
        }
    },

    //用户点击topTab时调用
    topTapClick(e) {
        this.setData({ topTapCurrentIndex: e.currentTarget.dataset.index })
        let index = e.currentTarget.dataset.index
        switch (index) {
            case '0':
                this.allPush()
                break;
            case '1':
                this.pendingReview()
                break;
            default:
                break;
        }
    },

    // 跳页到详细页面
    goDetailed(event) {
        let itemId = event.currentTarget.dataset.item.id
        wx.navigateTo({
            url: '/pages/home/tutor/tutorDetailed/tutorDetailed?itemId=' + itemId
        });
    },

    // 认证
    toAuthenticate() {
        wx.navigateTo({
            url: '/pages/detail/authenticate/authenticate'
          });
    },

    // App 默认方法

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ lang: app.globalData.lang, navTitle: "tutorInfo", navigation: true })
        this.homePage()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.currentIndex == 0){
            if(this.data.pageNo == this.data.totalPage) return
            let pageNo = this.data.pageNo += 1
            this.setData({pageNo : pageNo})
            this.homePage();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})